//
//  XSToastView.m
//  XSDownloadDemo
//
//  Created by 大帅 on 2023/12/25.
//

#import "XSToastView.h"

@interface XSToastView ()
/// 背景遮罩
@property (nonatomic, strong) UIView *toastBackView;

@end

@implementation XSToastView

static XSToastView *_sharedSingleton = nil;

/// 获取单例
+ (instancetype)shareManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 不能再使用 alloc 方法
        // 因为已经重写了 allocWithZone 方法，所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

// 防止 [[A alloc] init] 和 new 引起的错误。因为 [[A alloc] init] 和 new 实际是一样的工作原理，都是执行了下面方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [XSToastView shareManager];
}

// NSCopying 防止 copy 引起的错误。当你的单例类不遵循 NSCopying 协议，外部调用本身就会出错.
- (id)copyWithZone:(nullable NSZone *)zone {
    return [XSToastView shareManager];
}

// 防止 mutableCopy 引起的错误，当你的单例类不遵循 NSMutableCopying 协议，外部调用本身就会出错.
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [XSToastView shareManager];
}

/// 显示文字，默认1.5秒后消失
- (void)showMessage:(NSString *)message {
    [self showMessage:message duration:1.5];
}

/// 显示文字，设置显示时长
- (void)showMessage:(NSString *)message duration:(NSTimeInterval)duration {
    // 先销毁上一次的提示
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    if (self.toastBackView) {
        [self.toastBackView removeFromSuperview];
        self.toastBackView = nil;
    }
    
    // 提示遮罩
    self.toastBackView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.toastBackView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.3];
    [[UIApplication sharedApplication].windows.firstObject addSubview:self.toastBackView];
    [[UIApplication sharedApplication].windows.firstObject bringSubviewToFront:self.toastBackView];
    
    // 提示文字背景
    UIView *labelBackView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    labelBackView.backgroundColor = UIColor.blackColor;
    labelBackView.layer.cornerRadius = 8;
    labelBackView.layer.masksToBounds = YES;
    [self.toastBackView addSubview:labelBackView];
    [labelBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.toastBackView);
    }];
    
    // 提示文字
    UILabel *toastLabel = [[UILabel alloc] init];
    toastLabel.text = message;
    toastLabel.textColor = UIColor.whiteColor;
    toastLabel.font = [UIFont systemFontOfSize:15];
    toastLabel.numberOfLines = 0;
    [self.toastBackView addSubview:toastLabel];
    [toastLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(labelBackView).insets(UIEdgeInsetsMake(12, 17, 12, 17));
        make.width.mas_lessThanOrEqualTo(self.toastBackView.frame.size.width / 3 * 2);
        make.height.mas_lessThanOrEqualTo(self.toastBackView.frame.size.height / 2);
    }];
    
    // 隐藏提示
    [self performSelector:@selector(hidToastView) withObject:nil afterDelay:duration];
    
}

// 隐藏提示
- (void)hidToastView {
    if (self.toastBackView) {
        [self.toastBackView removeFromSuperview];
        self.toastBackView = nil;
    }
}

@end
